'''
Created on May 25, 2012

@author: thomas_dao
'''

from web import base
from web.base import BaseHandler
from web import urls
from entity.entity import Album, Event, Photo
from web import util
from app.query import photo_async, album_async
from google.appengine.ext import ndb
from google.appengine.ext.ndb import model
from webapp2_extras.appengine.auth.models import User
import json
import logging


class CreateHandler(BaseHandler):
    @base.user_required
    def post(self):
        """
        @param title: album title
        @param description: album description  
        """
        try:
            album = Album()
            album.author = util.get_current_user_key()
            album.description = self.request.params.get('description')
            album.title = self.request.params.get('title')
            event_id = self.request.params.get('event_id')
            if (event_id):
                event = Event.get_by_id(int(event_id))
                album.event = event.key
            album.put()
        except Exception as e: 
            logging.warn("Exception: %s" %e)
        # Write response back
        self.response.write(json.dumps(util.ndb_to_dict(album)))
    

class ReadHandler(BaseHandler):
    def get(self):
        album_id = self.request.params.get('id')
        album = Album.get_by_id(int(album_id))
        if album:
            self.response.write(json.dumps(util.ndb_to_dict(album)))
    

class UpdateHandler(BaseHandler):
    def post(self):
        """
        @param title: album title
        @param description: album description  
        """
        try:
            album_id = self.request.params.get('id')
            album = Album.get_by_id(int(album_id))
            album.author = util.get_current_user_key()
            album.description = self.request.params.get('description')
            album.title = self.request.params.get('title')
            event_id = self.request.params.get('event_id')
            if (event_id):
                event = Event.get_by_id(int(event_id))
                album.event = event.key
            album.put()
            updated = 1
        except Exception as e: 
            logging.warn("Exception: %s" %e)
            updated = 0
        # Write response back
        self.response.write(updated)


class DeleteHandler(BaseHandler):
    def post(self):
        pass    


class UserAlbumHandler(BaseHandler):
    """
    Return all album of user
    """
    @ndb.synctasklet
    def get(self, user_id):
        user_key = model.Key(User, int(user_id))
        albums = yield album_async.get_by_author(user_key)
        # Write response back
        self.response.write(json.dumps(util.ndb_to_list(albums)))        


class EventAlbumHandler(BaseHandler):
    """
    Return all album of event
    """
    @ndb.synctasklet
    def get(self, event_id):
        """
        @param event_id: id of existing event 
        """
        event_key = model.Key(Event, int(event_id))
        albums = yield album_async.get_by_event(event_key)
        # Write response back
        self.response.write(json.dumps(util.ndb_to_list(albums)))    


class AlbumPhotosHandler(BaseHandler):
    """
    Return all photos within an album
    """
    @ndb.synctasklet    
    def get(self, album_id):
        """
        @param album_id: id of existing album 
        """
        album_key = model.Key(Album, int(album_id))
        photos = yield photo_async.get_by_album(album_key)
        # Write response back
        self.response.write(json.dumps(util.ndb_to_list(photos)))


class CoverAlbumHandler(BaseHandler):
    """
    Return all photos in cover album
    """
    @ndb.synctasklet    
    def get(self, event_id):
        """
        @param event_id: id of existing event 
        """
        event_key = model.Key(Event, int(event_id))
        photos = yield photo_async.get_cover_photos_by_event(event_key)
        # Write response back
        self.response.write(json.dumps(util.ndb_to_list(photos)))

class DefaultAlbumHandler(BaseHandler):
    """
    Return all photos in default album
    """
    @ndb.synctasklet    
    def get(self, event_id):
        """
        @param event_id: id of existing event 
        """
        event_key = model.Key(Event, int(event_id))
        photos = yield photo_async.get_default_photos_by_event(event_key)
        # Write response back
        self.response.write(json.dumps(util.ndb_to_list(photos)))
        
        
app = urls.app

def main():
    app.run()

if __name__ == '__main__':
    main()